package dev

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"gowww/comeon/config"
	"log"
)

var DB *gorm.DB

func Init() {
	// 连接db
	var err error
	conf := config.GetConfigs()
	DB, err = gorm.Open("mysql", fmt.Sprintf("%v:%v@tcp(%v:%d)/%v?charset=%v&parseTime=True&loc=Local",
		conf.Mysql.Username,
		conf.Mysql.Password,
		conf.Mysql.Host,
		conf.Mysql.Port,
		conf.Mysql.Dbname,
		conf.Mysql.Charset,
	))
	if err != nil {
		log.Fatal(err.Error())
	}
	if DB.Error != nil {
		log.Fatal(DB.Error)
	}
}

func Close() {
	DB.Close()
}
